package com.hyq.market.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by XLee on 2017/6/14.
 */

public class TimeUtils {

    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.CHINA);
    private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    static {
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    /** 服务器时间转换成格式
     * 2017-06-06T03:01:11.000Z
     * 2017-06-06 03:01:11
     **/
    public static String convertServerTimeToString(String dateStr) {
        // 骑行记录调用，列表加载，format静态创建
        long time = 0;
        try {

            time = dateFormat.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date = new Date(time);
        return format.format(date);
    }

    /** 服务器时间转换成格式
     * 2017-06-06T03:01:11.000Z
     * 2017-06-06 03:01:11
     **/
    public static String convertServerTimeToString(String dateStr, String patten) {
        long time = 0;
        try {
            time = dateFormat.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(patten);
        return format.format(date);
    }

    /** 服务器时间转换成时间戳 2017-06-06T03:01:11.000Z*/
    public static long convertServerTime(String dateTime) {
        try {
            return dateFormat.parse(dateTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /** 两个服务器时间转换成分钟字符串*/
    public static String convert2ServerTimeMinutes(String startStr, String endStr) {
        try {
            long startTime = dateFormat.parse(startStr).getTime();
            long endTime = dateFormat.parse(endStr).getTime();

            long delayTime = Math.abs(endTime - startTime) / 1000;//除以1000是为了转换成秒

            if(delayTime % 60 != 0) return String.valueOf(delayTime / 60 + 1);
            return String.valueOf(delayTime / 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }

    /** 两个服务器时间转换成分钟字符串 5分钟30秒*/
    public static String convert2ServerTime(String startStr, String endStr) {
        try {
            long startTime = dateFormat.parse(startStr).getTime();
            long endTime = dateFormat.parse(endStr).getTime();

            long delayTime = Math.abs(endTime - startTime) / 1000;//除以1000是为了转换成秒

            long second = delayTime % 60;
            if(second == 0) return String.format("%d分钟", delayTime / 60);
            else return String.format("%1$d分钟%2$d秒", delayTime / 60, second);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0分钟";
    }

    /** 两个服务器时间转换成小时和分钟字符串*/
    public static String convert2ServerTimePlus(String startStr, String endStr) {

        try {
            long startTime = dateFormat.parse(startStr).getTime();
            long endTime = dateFormat.parse(endStr).getTime();

            long delayTime = Math.abs(endTime - startTime) / 1000;//除以1000是为了转换成秒

            long day1 = delayTime / (24 * 3600);
            long hour1 = delayTime % (24 * 3600) / 3600;
            long mm = delayTime % 3600 / 60;
            long second = delayTime % 60;

            if(second > 0) mm += 1;
            return checkHour(day1,hour1) + ":" + checkMM(mm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "00:00";
    }

    /**
     * 给定毫秒值获取分钟字符串
     */
    public static String convertMinute(long mills) {
        return String.valueOf(mills / 1000 / 60);
    }

    public static String checkHour(long day, long hour) {
        long dayHour = day * 24;
        hour = hour + dayHour;
        String hourStr;
        if (hour >= 0 && hour <= 9) {
            hourStr = "0" + hour;
        } else {
            hourStr = hour + "";
        }
        return hourStr;
    }

    public static String checkMM(long MM) {
        String MMStr;
        if (MM >= 0 && MM <= 9) {
            MMStr = "0" + MM;
        } else {
            MMStr = MM + "";
        }
        return MMStr;
    }

}
